﻿using System;

namespace Com.Core.Helps
{
    public class DateTimeHelper
    {
        public static readonly string[] WeekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        public static string Week(DateTime str)
        {
            string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            string week = weekdays[Convert.ToInt32(str.DayOfWeek)];
            return week;
        }

        public static string ShortWeek(DateTime str)
        {
            return Week(str).Replace("星期", "");
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static int ConvertDateTimeInt(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public static string GetReduce(DateTime endTime)
        {
            TimeSpan ts = endTime - DateTime.Now;
            string str;
            if (ts.Minutes < 1)
            {
                str = "0";
            }
            else
            {
                int day, hour, min;
                if (ts.Days > 30)
                {
                    var month = ts.Days/30;
                    day = ts.Days - 30*month;
                    str = $"{month}个月{day}天";
                }
                else if (ts.Days == 0)
                {
                    if (ts.Hours == 0)
                    {
                        min = ts.Minutes;
                        var sec = ts.Seconds;
                        str = $"{min}分钟{sec}秒";
                    }
                    else
                    {
                        hour = ts.Hours;
                        min = ts.Minutes;
                        str = $"{hour}小时{min}分钟";
                    }
                }
                else
                {
                    day = ts.Days;
                    hour = ts.Hours;
                    min = ts.Minutes;
                    str = $"{day}天";
                    if (hour > 0) str += $"{hour}小时";
                    else str += $"{min}分钟";
                }
            }

            return str;
        }
    }
}
